<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<div id="wrapper">
    <div class="main clearfix">
        <div class="col_730">        
            <div align="center">
                <h2>Set Busy</h2>
                <table border="0">
                    <form:form action="${baseUrl}/cp/saveteacher.html" method="POST" commandName="teacherFormAdd">
                        <tr>
                            <td class="slot_title">Teachers : </td>
                            <td colspan="6">
                                <select id="teachers" name="teachers" onchange="v_fShowBusy();">
                                    <option value="">Select a teacher</option>
                                    <c:forEach var="teacher" items="${teachers}">
                                        <option class="teacher" value="${teacher.teacher_id}" >${teacher.name}</option>
                                    </c:forEach>
                                </select>
                            </td>
                        </tr>
                            <td class="slot_title">Sessions : </td>
                            <td colspan="6">
                                <select id="sessions" name="sessions" onchange="v_fShowBusy();">
                                    <option value="">Select a session</option>
                                    <c:forEach var="session" items="${sessions}">
                                        <option class="session" value="${session.session_id}" >${session.year} - ${session.semester}</option>
                                    </c:forEach>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="slot_title">Day : </td>
                            <td colspan="6">
                                <select id="day" name="day" onchange="v_fShowBusy();">
                                    <option value="" >Select a day</option>
                                    <option value="2" >Monday</option>
                                    <option value="3" >Tuesday</option>
                                    <option value="4" >Wednesday</option>
                                    <option value="5" >Thursday</option>
                                    <option value="6" >Friday</option>
                                    <option value="7" >Saturday</option>
                                    <option value="0" >Sunday</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="slot_title">Slot : </td>
                            <td class="slot"> <span>1</span> </td>
                            <td class="slot"> <span>2</span> </td>
                            <td class="slot"> <span>3</span> </td>
                            <td class="slot"> <span>4</span> </td>
                            <td class="slot"> <span>5</span> </td>
                            <td class="slot"> <span>6</span> </td>
                        </tr>
                        <tr id="morning">
                            <td class="slot_title">Morning : </td>
                            <td class="slot"><input type="checkbox" id="morning_slot1" name="morning_slot1" value="1"/></td>
                            <td class="slot"><input type="checkbox" id="morning_slot2" name="morning_slot2" value="2"/></td>
                            <td class="slot"><input type="checkbox" id="morning_slot3" name="morning_slot3" value="3"/></td>
                            <td class="slot"><input type="checkbox" id="morning_slot4" name="morning_slot4" value="4"/></td>
                            <td class="slot"><input type="checkbox" id="morning_slot5" name="morning_slot5" value="5"/></td>
                            <td class="slot"><input type="checkbox" id="morning_slot6" name="morning_slot6" value="6"/></td>
                        </tr>
                        <tr id="afternoon">
                            <td class="slot_title">Afternoon : </td>
                            <td class="slot"><input type="checkbox" id="afternoon_slot1" name="afternoon_slot1" value="1"/></td>
                            <td class="slot"><input type="checkbox" id="afternoon_slot2" name="afternoon_slot2" value="2"/></td>
                            <td class="slot"><input type="checkbox" id="afternoon_slot3" name="afternoon_slot3" value="3"/></td>
                            <td class="slot"><input type="checkbox" id="afternoon_slot4" name="afternoon_slot4" value="4"/></td>
                            <td class="slot"><input type="checkbox" id="afternoon_slot5" name="afternoon_slot5" value="5"/></td>
                            <td class="slot"><input type="checkbox" id="afternoon_slot6" name="afternoon_slot6" value="6"/></td>
                        </tr>
                        <tr>
                            
                            <td >
                                <form:hidden path="id" value="0"/>
                                <input type="button" onclick="formCancel('${baseUrl}/cp/teachers.html');" value="Cancel"/>
                            </td>
                            <td>
                                <input type="button" onclick="formSetBusy();" value="Change"/>
                            </td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </form:form>
                </table>
            </div> 
        </div> 
    </div>
</div>
<script type="text/javascript">
        sz_Response = "${sz_Response}";
        if (sz_Response !== "")
        {
            alert(sz_Response);
        }
        
        $(document).ready(function() {
            
        });
        
        function v_fShowBusy(){
            var i_TeacherId = $("select#teachers").val();
            var i_SessionId = $("select#sessions").val();
            var i_DayId = $("select#day").val();
            
             $("tr").find("input").attr("checked", false);
             
            if(i_TeacherId != '' && i_SessionId != '' && i_DayId != ''){
                
                //return false;
                var sz_SetBusyTeacherUrl = "${baseUrl}/cpservice/setbusy.json";
                $.ajax({
                    type: "POST",
                    url: sz_SetBusyTeacherUrl,
                    data: "i_TeacherId=" + i_TeacherId + "&i_SessionId=" + i_SessionId + "&i_DayId=" + i_DayId,
                    success: function(response) {
                        if (response.status) {
                            
                            var aSlots = response.data.split(";");
                            var aSlotMorning = aSlots[0].split(",");
                            var aSlotAfternoon = aSlots[1].split(",");
                            //alert("aSlotMorning :" +aSlotMorning.length + "-" + aSlotMorning);
                            //alert("aSlotAfternoon :" + aSlotAfternoon.length + "-" + aSlotAfternoon);
                            if(aSlotMorning.length > 0 && aSlotMorning != "")
                            {
                                for(var i = 0; i< aSlotMorning.length; i++)    
                                {
                                    if(aSlotMorning[i] != ""){
                                        $("tr#morning").find("input[value="+aSlotMorning[i]+"]").attr("checked", true);
                                    }
                                }
                            }else
                            {
                                //alert('1');
                                $("tr#morning").find("input").attr("checked", false);
                            }
                            
                            if(aSlotAfternoon.length > 0 && aSlotAfternoon != "")
                            {
                                for(var i = 0; i< aSlotAfternoon.length; i++)    
                                {
                                    if(aSlotAfternoon[i] != ""){
                                        $("tr#afternoon").find("input[value="+aSlotAfternoon[i]+"]").attr("checked", true);
                                    }
                                }
                            }else
                            {
                                //alert('2');
                                $("tr#afternoon").find("input").attr("checked", false);
                            }
                        } else {
                            $("tr#morning").find("input").attr("checked", false);
                            $("tr#afternoon").find("input").attr("checked", false);
                        }
                    },
                    error: function(e) {
                        $("tr#morning").find("input").attr("checked", false);
                        $("tr#afternoon").find("input").attr("checked", false);
                        alert('Error: ' + e);
                    }
                });
            }
        }
        
        
        function formSetBusy()
        {
            var i_TeacherId = $("select#teachers").val();
            var i_SessionId = $("select#sessions").val();
            var i_DayId = $("select#day").val();
            
            var iMorningSlot1 = $("input#morning_slot1").val();
            var iMorningSlot2 = $("input#morning_slot2").val();
            var iMorningSlot3 = $("input#morning_slot3").val();
            var iMorningSlot4 = $("input#morning_slot4").val();
            var iMorningSlot5 = $("input#morning_slot5").val();
            var iMorningSlot6 = $("input#morning_slot6").val();
            
            var iAfternoonSlot1 = $("input#afternoon_slot1").val();
            var iAfternoonSlot2 = $("input#afternoon_slot2").val();
            var iAfternoonSlot3 = $("input#afternoon_slot3").val();
            var iAfternoonSlot4 = $("input#afternoon_slot4").val();
            var iAfternoonSlot5 = $("input#afternoon_slot5").val();
            var iAfternoonSlot6 = $("input#afternoon_slot6").val();
            
            var sMorningSlot = "";
            var sAfternoonSlot = "";
            for(var i = 1; i<=6; i++){
                if($('input#morning_slot'+i).is(':checked')){ 
                    sMorningSlot += $("input#morning_slot"+i).val()+ ",";
                }
                
                if($('input#afternoon_slot'+i).is(':checked')){ 
                    sAfternoonSlot += $("input#afternoon_slot"+i).val() + ",";
                }
            }
            alert(sMorningSlot);
            alert(sAfternoonSlot);
            
            var sz_EditBusyTeacherUrl = "${baseUrl}/cpservice/editbusy.json";
            $.ajax({
                type: "POST",
                url: sz_EditBusyTeacherUrl,
                data: "i_TeacherId=" + i_TeacherId + "&i_SessionId=" + i_SessionId + "&i_DayId=" + i_DayId+ "&s_MornigSlots=" + sMorningSlot+ "&s_AfternoonSlots=" + sAfternoonSlot,
                success: function(response) {
                    if (response.status) {
                        alert(response.message);
                        window.location.href = "${baseUrl}/cp/teachers.html";
                    } else {
                        alert(response.message);
                    }
                },
                error: function(e) {
                    alert('Error: ' + e);
                }
            });
        }
        
        
</script>